package com.taotao.search.dao;

import com.taotao.common.pojo.SearchItem;
import com.taotao.common.pojo.SearchResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.SearchItemMapper;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class SearchDao {
    @Autowired
    private SolrServer solrServer;
    @Autowired
    private SearchItemMapper searchItemMapper;
    //根据查询条件查询商品的结果集
    public SearchResult search(SolrQuery query) throws Exception {
        //注入solrserver对象  spring管理
        //执行查询
        //获取结果集
        //遍历结果集
        //设置SearchResult属性
        SearchResult searchResult = new SearchResult();

        QueryResponse response = solrServer.query(query);
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();


        SolrDocumentList results = response.getResults();
        searchResult.setRecordCount(results.getNumFound());
        List<SearchItem> itemList = new ArrayList<>();
        for (SolrDocument document : results) {
            //将solrdocument中的属性一个一个地设置到searchItem中
            SearchItem searchItem = new SearchItem();
            searchItem.setCategory_name(document.get("item_category_name").toString());
            searchItem.setId(Long.parseLong(document.get("id").toString()));
            searchItem.setImage(document.get("item_image").toString());
            searchItem.setPrice((Long) document.get("item_price"));
            searchItem.setSell_point(document.get("item_sell_point").toString());

            List<String> list = highlighting.get(document.get("id")).get("item_title");
            if (list != null && list.size() > 0) {
                searchItem.setTitle(list.get(0));
            } else {
                searchItem.setTitle(document.get("item_title").toString());
            }

            itemList.add(searchItem);
        }
        searchResult.setItemList(itemList);

        return searchResult;
    }
    
    public TaotaoResult updateSearchItemById(Long itemId) throws Exception{
        SearchItem searchItem = searchItemMapper.getSearchItemById(itemId);
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", searchItem.getId().toString());
        document.addField("item_title", searchItem.getTitle());
        document.addField("item_sell_point", searchItem.getSell_point());
        document.addField("item_price", searchItem.getPrice());
        document.addField("item_image", searchItem.getImage());
        document.addField("item_category_name", searchItem.getCategory_name());
        document.addField("item_desc", searchItem.getItem_desc());

        solrServer.add(document);
        solrServer.commit();
        return TaotaoResult.ok();
    }
}
